import { Button, message, Modal, Space, TypographyText } from 'ant-design-vue'
import moment from 'moment'
import { defineComponent, nextTick, onMounted, ref } from 'vue'

import { userApi } from '@/apis'
import qzyApi from '@/apis/qzyApi'
import { ProTable, ProTableProps, ProTableRef } from '@/components'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import router from '@/router'
import { bus, optionsToMap } from '@/utils'

export default defineComponent({
  name: 'QzyRedeemOrder',
  setup() {
    const id = ref(0)
    const status = ref(0)
    const statusOptions = [
      { label: '下架', value: 0 },
      { label: '上架', value: 1 }
    ]
    const visible = ref(false)
    const statusMap = optionsToMap(statusOptions)
    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref<ProTableProps>({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        toolbarConfig: {
          perfect: false,
          slots: {
            buttons: 'toolbarButtons'
          }
        },
        proxyConfig: {
          form: true,
          props: {
            result: 'records',
            total: 'total'
          },
          ajax: {
            query: async ({ page, form }) => {
              const data = await qzyApi.getQzyCombinationList({
                pageSize: page.pageSize,
                pageNo: page.currentPage,
                ...form
              })
              return data
            }
          }
        },
        formConfig: {
          items: [
            {
              title: '商品状态',
              field: 'coStatus',
              itemRender: {
                name: TABLE_ITEM_RENDER.ASelect,
                props: {
                  placeholder: '全部',
                  allowClear: true
                },
                options: statusOptions
              }
            },
            {
              title: '套装名称',
              field: 'title',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入活动名称',
                  style: {
                    width: '200px'
                  }
                }
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columnConfig: {
          minWidth: 100,
          resizable: true
        },
        columns: [
          {
            title: 'id',
            field: 'id'
          },
          {
            title: '组合套装名称',
            field: 'title'
          },
          {
            title: '套餐原价（元）',
            field: 'totalPrice'
          },
          {
            title: '套餐优惠价（元）',
            field: 'salePrice'
          },
          {
            title: '排序',
            type: 'seq'
          },
          {
            title: '状态',
            slots: {
              default: 'status'
            }
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            }
          }
        ],
        pagerConfig: {
          pageSize: 10
        }
      }
    })
    //刷新
    async function refresh() {
      await tableRef.value.table.commitProxy('query')
    }
    onMounted(() => {
      bus.on('refreshFormationList', () => {
        nextTick(() => {
          tableRef.value.refresh()
        })
      })
    })
    //新增
    function handleCreate() {
      router.push({ name: 'QzyGoodsEdit' })
    }
    //编辑
    function handleEdit(row) {
      router.push({ name: 'QzyGoodsEdit', query: { id: row.id } })
    }
    //查看详情
    function handleDetail(row) {
      router.push({ name: 'QzyGoodsDetail', query: { id: row.id } })
    }
    //上下架
    function handleStatus(row) {
      visible.value = true
      if (row.coStatus === 1) {
        status.value = 0
      } else {
        status.value = 1
      }
      id.value = row.id
    }
    async function handleOk(e) {
      console.log(e)
      try {
        await qzyApi
          .qzyOpeCombination({
            coStatus: status.value,
            id: id.value
          })
          .then(() => {
            visible.value = false
            message.success(status.value === 0 ? '下架成功' : ' 上架成功')
            refresh()
          })
      } finally {
      }
    }
    return {
      visible,
      tableRef,
      tableConfig,
      status,
      refresh,
      handleCreate,
      handleStatus,
      handleOk,
      handleDetail,
      handleEdit
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            status: ({ row }) => {
              return (
                <Space>
                  {row.coStatus == 0 ? (
                    <TypographyText type="danger">下架</TypographyText>
                  ) : (
                    <TypographyText type="success">上架</TypographyText>
                  )}
                </Space>
              )
            },
            operation: ({ row }) => (
              <Space>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </Button>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleStatus(row)}
                >
                  {row.coStatus === 0 ? '上架' : '下架'}
                </Button>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleDetail(row)}
                >
                  查看详情
                </Button>
              </Space>
            ),
            toolbarButtons: () => {
              return (
                <Space>
                  <Button
                    style={{ position: 'absolute', left: '10px', top: '5px' }}
                    type="primary"
                    onClick={this.handleCreate}
                  >
                    新增组合套餐
                  </Button>
                </Space>
              )
            }
          }}
          {...this.tableConfig}
        />
        <Modal
          v-model:visible={this.visible}
          title={this.status === 0 ? '下架' : ' 上架'}
          destroyOnClose
          onOk={this.handleOk}
          width={325}
        >
          确定{this.status === 0 ? '下架' : ' 上架'}该商品吗？
        </Modal>
      </div>
    )
  }
})
